<template>
	<div class="AdminList adminListarea">
		<div class="loadBox"></div>
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/' }">
				<i class="lh lh-sys fw_100"></i>
			</el-breadcrumb-item>
			<el-breadcrumb-item>系统设置</el-breadcrumb-item>
			<el-breadcrumb-item>人员管理</el-breadcrumb-item>
		</el-breadcrumb>
		<div class="content_Box box-shadow mt_15">
			<!-- <div class="mb_10 align-left pl_10">
				<span class="addOK" @click="addAdminBtn">新增人员</span>
			</div> -->
			<div class="py_5 px_10">
				<el-row>
					<el-col :span="2">
						<el-button type="success" class="fl" @click="addAdminBtn()" round size="mini">新增人员</el-button>
					</el-col>
					<el-col :span="22">
						<el-form :inline="true" class="form-inline fr" @keyup.enter.native="shearchBtn()">
							<el-form-item>
								<el-input v-model="query.username" placeholder="账号"></el-input>
							</el-form-item>
							<el-form-item>
								<el-input v-model="query.nickname" placeholder="姓名"></el-input>
							</el-form-item>
							<el-form-item>
								<el-input v-model="query.mobile" placeholder="手机号"></el-input>
							</el-form-item>
							<el-form-item class="w_100">
								<el-select v-model="query.role_id" placeholder="角色">
									<el-option
										v-for="(item, index) in roleArr"
										:key="index"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
							<el-button size="mini" type="primary" class="round-right" native-type="button" @click="shearchBtn"
								>搜索</el-button
							>
							<el-button size="mini" round @click="resetDataBtn">重置</el-button>
						</el-form>
					</el-col>
				</el-row>
			</div>
			<div class="tableMaxBox">
				<template>
					<el-table :data="adminList" stripe>
						<el-table-column prop="username" :show-overflow-tooltip="true" label="登录账号"></el-table-column>
						<el-table-column prop="nickname" :show-overflow-tooltip="true" label="姓名"></el-table-column>
						<el-table-column prop="mobile" label="手机号">
							<template slot-scope="{ row }">
								<span>{{ row.mobile | vf_mobile }}</span>
							</template>
						</el-table-column>
						<!-- <el-table-column
                            prop="enterprise_name"
                            :show-overflow-tooltip="true"
                            label="所属部门"
                        ></el-table-column> -->
						<el-table-column prop="role_name" label="角色"></el-table-column>
						<el-table-column prop="email" label="Email" width="220px" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="状态" width="80px">
							<template slot-scope="{ row }">
								<span v-if="row.status == '1'" style="color: #28c76f;">启用</span>
								<span v-if="row.status == '2'" style="color: #d0121b;">停用</span>
							</template>
						</el-table-column>
						<el-table-column prop="last_login" label="最后登录时间" width="150px"></el-table-column>
						<el-table-column prop="ip" label="ip" width="130px"></el-table-column>
						<el-table-column fixed="right" label="操作" width="240px">
							<template slot-scope="{ row }">
								<a class="cz-font pointer cz-jd" @click="redactBtn(row)">编辑</a>
								<a class="cz-font pointer cz-jd" @click="redactPasswordBtn(row)">修改密码</a>
								<a class="cz-font pointer" v-if="row.status == '1'" style="color: #d0121b;" @click="disuse(row, 1)"
									>停用</a
								>
								<a class="cz-font pointer" v-if="row.status == '2'" style="color: #28c76f;" @click="disuse(row, 2)"
									>启用</a
								>
								<a class="cz-font pointer cz-jd" @click="viewQRCode(row)">C二维码</a>
								<a class="cz-font pointer cz-jd" @click="viewQRCodeB(row)">B二维码</a>
							</template>
						</el-table-column>
					</el-table>
					<!-- 分页组件 -->
					<page :total="total" :pageNums="pageNum" :disableMountedFetch="true" @pagedata="onChildValue"></page>
				</template>
			</div>
		</div>
		<!-- 添加管理员 -->
		<el-dialog
			title="新增管理员"
			:visible.sync="addAdminShow"
			width="560px"
			:before-close="close"
			:close-on-click-modal="false"
		>
			<div class="auditCentent">
				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>登录账号：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.username" placeholder="请输入"></el-input>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>角色：</span>
					</div>
					<div class="fl list-cont">
						<el-select v-model="addAdmin.role_id" placeholder="请选择" @change="roleChange">
							<el-option v-for="item in adminRoleList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</div>
				</div>
				<div class="row-info clear" v-if="addAdmin.role_id == 50">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>代理商：</span>
					</div>
					<div class="fl list-cont">
						<el-select v-model="addAdmin.agent_id" placeholder="请选择" clearable filterable @change="agentChange">
							<el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>登录密码：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.password" placeholder="八位以上包含数字、大小写字母"></el-input>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>姓名：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.nickname" placeholder="请输入"></el-input>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span>性别：</span>
					</div>
					<div class="fl list-cont">
						<el-radio-group v-model="addAdmin.sex">
							<el-radio label="1">男</el-radio>
							<el-radio label="2">女</el-radio>
						</el-radio-group>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span>身份：</span>
					</div>
					<div class="fl list-cont">
						<el-radio-group v-model="addAdmin.rank">
							<el-radio label="负责人">负责人</el-radio>
							<el-radio label="职员">职员</el-radio>
						</el-radio-group>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span>职务：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.job" placeholder="请输入"></el-input>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span> <i class="lhadmin lhadmin-wujiaoxing"></i>手机号码： </span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.mobile" maxlength="11" placeholder="请输入"></el-input>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<span>邮箱：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.email" placeholder="请输入"></el-input>
					</div>
				</div>
				<div class="datarights">
					<p>
						销售工具数据权限
					</p>
					<div v-for="(item, index) in allCities" :key="'only' + index" style="margin: 20px 0;">
						<el-checkbox
							:indeterminate="item.isIndeterminate"
							v-model="item.selected"
							@change="handleCheckAllChange(item.selected, index)"
							>{{ item.cityName }}</el-checkbox
						>
						<el-checkbox-group v-model="item.checkedCities">
							<el-checkbox
								v-for="(city, sum) in item.list"
								:label="city.areaId"
								:key="sum"
								@change="handleCheckedCitiesChange(index, sum)"
								>{{ city.areaName }}</el-checkbox
							>
						</el-checkbox-group>
					</div>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="btnOK mr_20" @click="addAdminSubmit">确 定</span>
				<span class="resetBtn" @click="close">取 消</span>
			</div>
		</el-dialog>

		<!-- 编辑人员 -->
		<el-dialog
			title="编辑人员"
			:visible.sync="redactAdminShow"
			width="560px"
			:before-close="close"
			:close-on-click-modal="false"
		>
			<div class="auditCentent">
				<div class="row-info clear">
					<div class="fl title-key">
						<span>登录账号：</span>
					</div>
					<div class="fl list-cont">
						<span>{{ addAdmin.username }}</span>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>角色：</span>
					</div>
					<div class="fl list-cont">
						<el-select v-model="addAdmin.role_id" placeholder="请选择" @change="roleChange">
							<el-option v-for="item in adminRoleList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</div>
				</div>
				<div class="row-info clear" v-if="addAdmin.role_id == 50">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>代理商：</span>
					</div>
					<div class="fl list-cont">
						<el-select v-model="addAdmin.agent_id" placeholder="请选择" clearable filterable @change="agentChange">
							<el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<span>登录密码：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.password" placeholder="八位以上包含数字、大小写字母"></el-input>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>姓名：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.nickname" placeholder="请输入"></el-input>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span>性别：</span>
					</div>
					<div class="fl list-cont">
						<el-radio-group v-model="addAdmin.sex">
							<el-radio label="1">男</el-radio>
							<el-radio label="2">女</el-radio>
						</el-radio-group>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span>身份：</span>
					</div>
					<div class="fl list-cont">
						<el-radio-group v-model="addAdmin.rank">
							<el-radio label="负责人">负责人</el-radio>
							<el-radio label="职员">职员</el-radio>
						</el-radio-group>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span>职务：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.job" placeholder="请输入"></el-input>
					</div>
				</div>

				<div class="row-info clear">
					<div class="fl title-key">
						<span> <i class="lhadmin lhadmin-wujiaoxing"></i>手机号码： </span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.mobile" maxlength="11" placeholder="请输入"></el-input>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<span>邮箱：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.email" placeholder="请输入"></el-input>
					</div>
				</div>
				<div class="datarights">
					<p>
						销售工具数据权限
					</p>
					<div v-for="(item, index) in allCities" :key="'only' + index" style="margin: 20px 0;">
						<el-checkbox
							:indeterminate="item.isIndeterminate"
							v-model="item.selected"
							@change="handleCheckAllChange(item.selected, index)"
							>{{ item.cityName }}</el-checkbox
						>
						<el-checkbox-group v-model="item.checkedCities">
							<el-checkbox
								v-for="(city, sum) in item.list"
								:label="city.areaId"
								:key="sum"
								@change="handleCheckedCitiesChange(index, sum)"
								>{{ city.areaName }}</el-checkbox
							>
						</el-checkbox-group>
					</div>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="btnOK mr_20" @click="redactSubmit">确 定</span>
				<span class="resetBtn" @click="close">取 消</span>
			</div>
		</el-dialog>
		<!-- 修改密码 -->
		<el-dialog
			title="修改密码"
			:visible.sync="passwordShow"
			width="560px"
			:before-close="close"
			:close-on-click-modal="false"
		>
			<div class="auditCentent">
				<div class="row-info clear">
					<div class="fl title-key">
						<span>登录账号：</span>
					</div>
					<div class="fl list-cont">
						<span>{{ addAdmin.username }}</span>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>登录密码：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.password" placeholder="请输入"></el-input>
					</div>
				</div>
				<div class="row-info clear">
					<div class="fl title-key">
						<i class="lhadmin lhadmin-wujiaoxing"></i>
						<span>再次确认密码：</span>
					</div>
					<div class="fl list-cont">
						<el-input v-model="addAdmin.password1" placeholder="请输入"></el-input>
					</div>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="btnOK mr_20" @click="redactPwdSubmit">确 定</span>
				<span class="resetBtn" @click="close">取 消</span>
			</div>
		</el-dialog>
		<!-- 下属账号 -->
		<el-dialog
			title="下属账号"
			:visible.sync="salesShow"
			width="560px"
			:before-close="close"
			:close-on-click-modal="false"
		>
			<div class="salesBox">
				<div>
					<el-checkbox-group v-model="activeSalesArr">
						<el-checkbox v-for="item in salesList" :label="item.id" :key="item.id">{{ item.nickname }}</el-checkbox>
					</el-checkbox-group>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<span class="btnOK mr_20" @click="salesSubmit">确 定</span>
				<span class="resetBtn" @click="close">取 消</span>
			</div>
		</el-dialog>

		<!--二维码-->
		<el-dialog title="C端公众号二维码" :visible.sync="showQRCode" width="450px" @close="qrcode_url = ''">
			<h4 style="text-align: center;">{{ qrcode_name }}</h4>
			<img :src="qrcode_url" style="margin: 0 auto; display: block; width: 300px;" />
			<p style="font-size: 12px;">通过该二维码关注公众号的人才登录小程序，会将人才来源标记为该人员</p>
		</el-dialog>
		<el-dialog title="B端公众号二维码" :visible.sync="showQRCodeB" width="450px" @close="qrcode_urlB = ''">
			<h4 style="text-align: center;">{{ qrcode_nameB }}</h4>
			<img :src="qrcode_urlB" style="margin: 0 auto; display: block; width: 300px;" />
			<p style="font-size: 12px;">通过该二维码关注B端公众号的商户注册小程序会将店铺来源标记为该人员</p>
		</el-dialog>
	</div>
</template>
<script>
var common = require('../../static/tool.js')
import {
	fetchUserAreaList,
	fetchManagerQRCode,
	fetchManagerQRCodeB,
	fetchRoleList,
	fetchListUser,
	addManageUser,
	editManageUser
} from '@/api/user.js'
import { fetchAgentList, fetchCityAreaList } from '@/api/agent.js'
export default {
	name: 'AdminList',
	data() {
		return {
			manageInfo: JSON.parse(sessionStorage.getItem('manageInfo')),
			url: common.url,
			adminList: [], //管理员列表
			addAdminShow: false, //添加管理员弹框
			redactAdminShow: false, //编辑管理员
			passwordShow: false, //修改登录密码
			/*角色数组*/
			adminRoleList: [], //角色列表
			qrcode_url: '',
			qrcode_name: '',

			qrcode_urlB: '',
			qrcode_nameB: '',

			addAdmin: {
				id: '', //管理员id
				role_id: '', //角色ID
				username: '', //用户名
				password: '', //密码
				password1: '', //重复密码
				nickname: '', //昵称
				department: 0, //部门，目前组织架构没做传[0]
				mobile: '', //手机号
				enterprise_name: '',
				email: '', //邮箱
				sex: '', //性别[1]男[2]女
				job: '', //职位
				rank: '', //身份
				agent_id: '' //代理商
			}, //添加管理员
			agentList: [], //代理商列表
			query: {
				username: '',
				nickname: '',
				mobile: '',
				role_id: '',
				type: 1
			},
			roleArr: [],
			salesList: [], //销售列表
			activeSalesArr: [], //所属下级销售
			salesShow: false, //销售弹框
			parent_id: '', //销售总监id

			// 商圈数据权限
			checkAll: false,
			checkedCities: [],
			checkedCitiesnext: [],
			cities: [],
			isIndeterminate: true,
			allCities: [],
			//向分页组件传值
			total: 0,
			pageNum: 1,
			pageSizeNow: 20
		}
	},
	created() {
		if (this.$route.query.role_id) {
			this.query.role_id = +this.$route.query.role_id
			this.query.type = +this.$route.query.role_id === 50 ? 2 : 1
		}
	},
	mounted() {
		this.getRoleList()
		this.startResult()
	},
	computed: {
		showQRCode: {
			get() {
				return !!this.qrcode_url
			},
			set() {}
		},
		showQRCodeB: {
			get() {
				return !!this.qrcode_urlB
			},
			set() {}
		}
	},
	methods: {
		roleChange(val) {
			this.addAdmin.agent_id = ''
			if (+val === 50) {
				this.allCities = []
				this.getAgentList()
			}
		},
		agentChange(val) {
			console.log(val)
			this.allCities = []
			this.getuserAreaList(val)
		},
		// 获取代理商列表
		async getAgentList() {
			try {
				let ret = await fetchAgentList({
					status: 1,
					page: 1,
					limit: 10000
				})
				this.agentList = ret.list.map(it => {
					return {
						id: it.id,
						sn: it.sn,
						name: it.name
					}
				})
			} catch (err) {
				console.log('err:', err)
			}
		},
		async getRoleList() {
			try {
				let ret = await fetchRoleList()
				this.roleArr = ret.list
				let new_obj = []
				if (+JSON.parse(sessionStorage.getItem('manageInfo')).role_id != 1) {
					// 不显示超级管理员的选项
					ret.list.forEach(item => {
						if (item.name != '超级管理员') {
							new_obj.push(item)
						}
					})
				} else {
					new_obj = ret.list
				}
				this.adminRoleList = new_obj
			} catch (err) {
				console.log(err)
			}
		},
		startResult() {
			if (!sessionStorage.getItem('manageInfo')) {
				// 判断是否登录
				window.location = 'login.html'
				return
			}
			this.getEnterprise() //获取管理员列表
			//this.getListSales(); //获取销售列表
		},

		async getuserAreaList(agent_id) {
			try {
				let ret = null
				if (agent_id) {
					ret = await fetchCityAreaList({ agent_id, user_id: this.addAdmin.id })
				} else {
					ret = await fetchUserAreaList({
						id: this.addAdmin.id
					})
				}
				console.log(ret.list)
				if (ret.list.length) {
					ret.list.forEach((element, index) => {
						this.allCities.push({
							isIndeterminate: false,
							list: [],
							checkedCities: [],
							cityId: element.city_id,
							cityName: element.city_name,
							selected: false
						})
						element.data.forEach(item => {
							this.allCities[index].list.push({ areaId: item.id, areaName: item.area_name, selected: false })
							if (+item.is_have == 1) {
								this.allCities[index].checkedCities.push(item.id)
							}
						})

						if (
							this.allCities[index].checkedCities.length != 0 &&
							this.allCities[index].checkedCities.length == this.allCities[index].list.length
						) {
							this.allCities[index].selected = true
							this.allCities[index].isIndeterminate = false
						} else {
							this.allCities[index].selected = false
							this.allCities[index].isIndeterminate = true
						}
					})
				}
				console.log(this.allCities)
			} catch (err) {}
		},

		close() {
			//关闭弹框
			this.addAdminShow = false
			this.redactAdminShow = false
			this.passwordShow = false
			for (let key in this.addAdmin) {
				this.addAdmin[key] = ''
			}
			/*销售*/
			this.salesShow = false
			this.parent_id = ''

			this.allCities = []
		},
		shearchBtn() {
			this.pageNum = 1
			this.getEnterprise()
		},
		async getEnterprise() {
			//获取管理员列表
			try {
				let ret = await fetchListUser({
					pageSize: this.pageSizeNow,
					page: this.pageNum,
					...this.query
				})
				this.adminList = ret.list
				this.total = ret.count
			} catch (err) {}
		},
		resetDataBtn() {
			this.pageNum = 1
			this.query.username = ''
			this.query.nickname = ''
			this.query.mobile = ''
			this.query.role_id = ''
			this.getEnterprise()
		},
		//分页组件->子组件传值
		onChildValue(item) {
			this.pageNum = item[0]
			this.pageSizeNow = item[1]
			this.getEnterprise()
		},

		// 修改密码
		redactPasswordBtn(row) {
			this.addAdmin.id = row.user_id
			this.addAdmin.username = row.username
			this.passwordShow = true
		},

		getListSales() {
			//获取销售列表
			return
			let fn = new FormData()
			fn.append('index', '1')
			this.$http.post(this.url + '/admin/listSales', fn).then(
				res => {
					// console.log(res);
					if (res.data.msg == '查询成功' || res.data.code == '200') {
						if (res.data.data) {
							this.salesList = res.data.data
						}
					} else if (res.data.code == '8886') {
						//code   8886  登录过期
						sessionStorage.clear()
						this.$alert('登录已过期，请重新登录 !', '提示', {
							confirmButtonText: '确定',
							callback: action => {
								window.location = 'login.html'
							}
						})
						return
					} else {
						this.$message.error(res.data.msg)
						return
					}
				},
				res => {
					this.$message.error('网络错误！')
					return
				}
			)
		},

		lookMemberBtn(row) {
			//查看下属账号
			this.activeSalesArr = []
			this.parent_id = row.id
			let sales = this.salesList
			if (sales.length > 0) {
				for (let i = 0; i < sales.length; i++) {
					if (sales[i].parent_id == row.id) {
						this.activeSalesArr.push(sales[i].id)
					}
				}
			}
			this.salesShow = true
		},
		salesSubmit() {
			//提交下属账号
			let lowids = this.activeSalesArr.join(',')
			let fn = new FormData()
			fn.append('id', this.parent_id)
			fn.append('lowids', lowids)
			this.$http.post(this.url + '/admin/editLowLevel', fn).then(
				res => {
					if (res.data.msg == '查询成功' || res.data.code == '200') {
						this.$message.success(res.data.msg)
						this.close()
						this.getListSales() //获取销售列表
						this.getEnterprise() //获取管理员列表
					} else if (res.data.code == '8886') {
						//code   8886  登录过期
						sessionStorage.clear()
						this.$alert('登录已过期，请重新登录 !', '提示', {
							confirmButtonText: '确定',
							callback: action => {
								window.location = 'login.html'
							}
						})
						return
					} else {
						this.$message.error(res.data.msg)
						return
					}
				},
				res => {
					this.$message.error('网络错误！')
					return
				}
			)
		},

		// 修改密码
		redactPwdSubmit() {
			if (this.addAdmin.password == '' || this.addAdmin.password1 == '') {
				this.$message.error('请输入密码！')
				return
			}
			if (this.addAdmin.password !== this.addAdmin.password1) {
				this.$message.error('两次密码输入不一致！')
				return
			}
			let fn = new FormData()
			fn.append('id', this.addAdmin.id)
			fn.append('password', this.addAdmin.password)
			this.$http.post(this.url + '/manageUser/updatePassword', fn).then(
				res => {
					if (res.data.code == '200') {
						this.$message.success('修改成功！')
						this.close()
						this.getEnterprise() //获取管理员列表
					} else if (res.data.code == '8886') {
						//code   8886  登录过期
						sessionStorage.clear()
						this.$alert('登录已过期，请重新登录 !', '提示', {
							confirmButtonText: '确定',
							callback: action => {
								window.location = 'login.html'
							}
						})
						return
					} else {
						this.$message.error(res.data.msg)
						return
					}
				},
				res => {
					this.$message.error('网络错误！')
					return
				}
			)
		},

		// 添加管理员
		addAdminBtn() {
			this.addAdminShow = true
			this.getuserAreaList()
		},

		computedParams(params = {}) {
			const { agent_id, role_id, ...rest } = this.addAdmin

			return {
				...rest,
				role_id: +role_id,
				agent_id: +role_id === 50 ? agent_id : '',
				...params
			}
		},
		// 提交添加管理员
		async addAdminSubmit() {
			try {
				if (
					this.addAdmin.username == '' ||
					this.addAdmin.nickname == '' ||
					this.addAdmin.mobile == '' ||
					this.addAdmin.role_id == '' ||
					this.addAdmin.password == ''
				) {
					return this.$message.error('请填写必填项')
				}
				if (+this.addAdmin.role_id === 50) {
					if (this.addAdmin.agent_id == '') return this.$message.error('请填写必填项')
				}
				const reqParams = this.computedParams()
				let city_id = []
				let area_id = []
				this.allCities.forEach(it => {
					if (it.checkedCities.length > 0) {
						city_id.push(it.cityId)
						area_id = [...area_id, ...it.checkedCities]
					}
				})
				await addManageUser({
					role_id: reqParams.role_id,
					username: reqParams.username,
					password: reqParams.password,
					nickname: reqParams.nickname,
					sex: reqParams.sex,
					rank: reqParams.rank,
					job: reqParams.job,
					mobile: reqParams.mobile,
					email: reqParams.email,
					department: 0,
					agent_id: reqParams.agent_id,
					area: area_id.join(','),
					area_id: area_id.join(','),
					city_id: city_id.join(',')
				})
				this.$message.success(!reqParams.id ? '添加成功' : '修改成功')
				this.close()
				this.getListSales() //获取销售列表
				this.getEnterprise() //获取管理员列表
			} catch (err) {
				console.log(err)
			}
		},

		//编辑
		redactBtn(row) {
			this.addAdmin.id = row.user_id
			this.addAdmin.username = row.username
			this.addAdmin.nickname = row.nickname
			this.addAdmin.sex = row.sex.toString()
			this.addAdmin.rank = row.rank
			this.addAdmin.job = row.job
			this.addAdmin.nickname = row.nickname
			this.addAdmin.mobile = row.mobile
			this.addAdmin.role_id = parseInt(row.role_id)
			this.addAdmin.email = row.email
			this.addAdmin.agent_id = row.agent_id
			this.addAdmin.type = row.type
			this.redactAdminShow = true
			if (row.agent_id == 0) this.getuserAreaList()
			if (row.agent_id > 0) this.getuserAreaList(row.agent_id)

			console.log(row)
			if (+row.role_id === 50) this.getAgentList()
		},
		async redactSubmit() {
			try {
				if (this.addAdmin.nickname == '' || this.addAdmin.mobile == '' || this.addAdmin.role_id == '') {
					return this.$message.error('请填写必填项')
				}
				if (+this.addAdmin.role_id === 50) {
					if (this.addAdmin.agent_id == '') return this.$message.error('请填写必填项')
				}
				let city_id = []
				let area_id = []
				this.allCities.forEach(it => {
					if (it.checkedCities.length > 0) {
						city_id.push(it.cityId)
						area_id = [...area_id, ...it.checkedCities]
					}
				})
				await editManageUser({
					id: this.addAdmin.id,
					role_id: this.addAdmin.role_id,
					username: this.addAdmin.username,
					nickname: this.addAdmin.nickname,
					sex: this.addAdmin.sex,
					rank: this.addAdmin.rank,
					job: this.addAdmin.job,
					mobile: this.addAdmin.mobile,
					email: this.addAdmin.email,
					department: this.addAdmin.department,
					password: this.addAdmin.password,
					agent_id: this.addAdmin.role_id != 50 ? 0 : this.addAdmin.agent_id,
					type: this.addAdmin.role_id != 50 ? 1 : 2,
					area: area_id.join(','),
					area_id: area_id.join(','),
					city_id: city_id.join(',')
				})
				this.$message.success('修改成功')
				this.close()
				this.getListSales() //获取销售列表
				this.getEnterprise() //获取管理员列表
			} catch (err) {
				console.log(err)
			}
		},

		async viewQRCode(row) {
			try {
				let ret = await fetchManagerQRCode({
					user_id: row.user_id,
					nickname: row.nickname
				})

				this.qrcode_url = ret
				this.qrcode_name = row.nickname
			} catch (err) {}
		},

		// 获取B公众号二维码
		async viewQRCodeB(row) {
			try {
				let ret = await fetchManagerQRCodeB({
					user_id: row.user_id,
					nickname: row.nickname
				})
				console.log(ret)
				this.qrcode_urlB = ret
				this.qrcode_nameB = row.nickname
			} catch (err) {}
		},

		//状态切换
		disuse(row, num) {
			let status
			if (num == 1) {
				status = 2
				this.$confirm('停用账号后，用户将无法登录系统', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						let fn = new FormData()
						fn.append('id', row.user_id)
						fn.append('status', status)
						this.$http.post(this.url + '/manageUser/userOperate', fn).then(
							res => {
								if (res.data.code == '200') {
									this.$message.success(res.data.msg)
									this.close()
									this.getListSales() //获取销售列表
									this.getEnterprise() //获取管理员列表
								} else if (res.data.code == '8886') {
									//code   8886  登录过期
									sessionStorage.clear()
									this.$alert('登录已过期，请重新登录 !', '提示', {
										confirmButtonText: '确定',
										callback: action => {
											window.location = 'login.html'
										}
									})
									return
								} else {
									this.$message.error(res.data.msg)
									return
								}
							},
							res => {
								this.$message.error('网络错误！')
								return
							}
						)
					})
					.catch(() => {})
			} else if (num == 2) {
				status = 1
				this.$confirm('启用账号，用户可正常登录系统', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						let fn = new FormData()
						fn.append('id', row.user_id)
						fn.append('status', status)
						this.$http.post(this.url + '/manageUser/userOperate', fn).then(
							res => {
								if (res.data.code == '200') {
									this.$message.success(res.data.msg)
									this.close()
									this.getListSales() //获取销售列表
									this.getEnterprise() //获取管理员列表
								} else if (res.data.code == '8886') {
									//code   8886  登录过期
									sessionStorage.clear()
									this.$alert('登录已过期，请重新登录 !', '提示', {
										confirmButtonText: '确定',
										callback: action => {
											window.location = 'login.html'
										}
									})
									return
								} else {
									this.$message.error(res.data.msg)
									return
								}
							},
							res => {
								this.$message.error('网络错误！')
								return
							}
						)
					})
					.catch(() => {})
			}
		},

		// 删除管理员
		deleteAdmin(row) {
			this.$confirm('操作将删除此管理员, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					let fn = new FormData()
					fn.append('id', row.id)
					this.$http.post(this.url + '/admin/deleteAdmin', fn).then(
						res => {
							if (res.data.code == '200') {
								this.close()
								this.getListSales() //获取销售列表
								this.getEnterprise() //获取管理员列表
							} else if (res.data.code == '8886') {
								//code   8886  登录过期
								sessionStorage.clear()
								this.$alert('登录已过期，请重新登录 !', '提示', {
									confirmButtonText: '确定',
									callback: action => {
										window.location = 'login.html'
									}
								})
								return
							} else {
								this.$message.error(res.data.msg)
								return
							}
						},
						res => {
							this.$message.error('网络错误！')
							return
						}
					)
				})
				.catch(() => {})
		},

		handleCheckAllChange(val, index) {
			console.log(val, index)
			if (val) {
				this.allCities[index].list.forEach(item => {
					item.selected = true
					this.allCities[index].checkedCities.push(item.areaId)
				})
			} else {
				this.allCities[index].checkedCities = []
			}

			// this.checkedCities = val ? this.checkedCitiesnext : []

			this.allCities[index].isIndeterminate = false

			// this.checkedCities = val ? this.checkedCitiesnext : []
			// this.isIndeterminate = false
		},
		handleCheckedCitiesChange(index, sum) {
			console.log(index + ',+,' + sum)
			this.allCities[index].list[sum].selected = !this.allCities[index].list[sum].selected

			let checkedCount = this.allCities[index].checkedCities.length
			console.log('checkedCount', checkedCount)
			this.allCities[index].selected = checkedCount === this.allCities[index].list.length
			this.allCities[index].isIndeterminate = checkedCount > 0 && checkedCount < this.allCities[index].list.length
		}
	},
	filters: {}
}
</script>

<style scoped>
.dialog-footer {
	padding-left: 125px;
	text-align: left;
}
.row-info {
	height: 40px;
}
.title-key {
	width: 110px;
}
.salesBox {
	min-height: 200px;
}
@media (max-width: 767.78px) {
}

.datarights {
	padding: 0 30px;
}

.datarights p {
	line-height: 40px;
	font-size: 15px;
	font-weight: bold;
	border-top: 1px solid #c0c4cc;
	margin-top: 5px;
}

/* 上传 */
</style>



// WEBPACK FOOTER //
// src/components/AdminList.vue